home *** CD-ROM | disk | FTP | other *** search
Modula Implementation | 1995-03-10 | 6.6 KB | 325 lines | [TEXT/3PRM] |
- implementation module commonDef;
-
-
- //
- // Common types for the event I/O system:
- //
-
-
- import StdEnv;
- from mac_types import Rect, WindowPtr, Toolbox;
- from quickdraw import QGetPort, QSetPort, GrafPtr, QLocalToGlobal, QGlobalToLocal;
- from deltaPicture import Rectangle, Point;
-
-
- :: ItemTitle :== String;
-
- :: SelectState = Able | Unable;
- :: MarkState = Mark | NoMark;
-
- :: KeyboardState :== (!KeyCode, !KeyState, !Modifiers);
- :: KeyCode :== Char;
- :: KeyState = KeyUp | KeyDown | KeyStillDown;
-
- :: MouseState :== (!MousePosition, !ButtonState, !Modifiers);
- :: MousePosition :== (!Int, !Int);
- :: ButtonState = ButtonUp | ButtonDown
- | ButtonDoubleDown | ButtonTripleDown | ButtonStillDown;
-
- :: Modifiers :== (!Bool, !Bool, !Bool, !Bool);
- :: PictureDomain :== Rectangle;
-
-
-
- SelectStateEqual :: !SelectState !SelectState -> Bool;
- SelectStateEqual Able Able = True;
- SelectStateEqual Unable Unable = True;
- SelectStateEqual _ _ = False;
-
- MarkEqual :: !MarkState !MarkState -> Bool;
- MarkEqual Mark Mark = True;
- MarkEqual NoMark NoMark = True;
- MarkEqual _ _ = False;
-
- ButtonStateEqual :: !ButtonState !ButtonState -> Bool;
- ButtonStateEqual ButtonUp ButtonUp = True;
- ButtonStateEqual ButtonDown ButtonDown = True;
- ButtonStateEqual ButtonStillDown ButtonStillDown = True;
- ButtonStateEqual _ _ = False;
-
- Enabled :: !SelectState -> Bool;
- Enabled Able = True;
- Enabled Unable = False;
-
- Checked :: !MarkState -> Bool;
- Checked Mark = True;
- Checked NoMark = False;
-
- MarkSwitch :: !MarkState -> MarkState;
- MarkSwitch Mark = NoMark;
- MarkSwitch NoMark = Mark;
-
- /* Convert a KeyMap (returned by GetKeys) into Modifiers (four booleans). */
-
- KeyMapToModifiers :: !(!Int,!Int,!Int,!Int) -> Modifiers;
- KeyMapToModifiers (_,word,_,_)
- = (IntToBool shift, IntToBool option, IntToBool command, IntToBool control);
- where {
- shift = word bitand ShiftMask;
- option = word bitand OptionMask;
- command = word bitand CommandMask;
- control = word bitand ControlMask;
- };
-
- IntToBool :: !Int -> Bool;
- IntToBool 0 = False;
- IntToBool _ = True;
-
- /* Conversion of modifiers as found in events. */
-
- ModifiersToINT :: !Modifiers -> Int;
- ModifiersToINT (shift, option, command, control)
- = Mask shift 512 bitor
- (Mask option 2048 bitor
- (Mask command 256 bitor
- Mask control 4096));
-
- Mask :: !Bool !Int -> Int;
- Mask b n | b = n;
- = 0;
-
- INTToModifiers :: !Int -> Modifiers;
- INTToModifiers flags
- = (FlagIsSet flags 512, FlagIsSet flags 2048, FlagIsSet flags 256, FlagIsSet flags 4096);
-
- FlagIsSet flags flag :== (flags bitand flag) <> 0;
-
- ShiftMask :== 1;
- OptionMask :== 4;
- CommandMask :== 32768;
- ControlMask :== 8;
-
-
- /* Control structure rules: */
-
- InGrafport :: !WindowPtr !(Toolbox -> (x, Toolbox)) !Toolbox -> (!x, !Toolbox);
- InGrafport wPtr f tb
- = (x, tb4);
- where {
- (port,tb1) = QGetPort tb;
- tb2 = QSetPort wPtr tb1;
- (x, tb3) = f tb2;
- tb4 = QSetPort port tb3;
- };
-
- InGrafport2 :: !WindowPtr !(Toolbox -> Toolbox) !Toolbox -> Toolbox;
- InGrafport2 wPtr f tb
- = tb4;
- where {
- (port,tb1) = QGetPort tb;
- tb2 = QSetPort wPtr tb1;
- tb3 = f tb2;
- tb4 = QSetPort port tb3;
- };
-
-
- LocalToGlobal :: !Point !Toolbox -> (!Point,!Toolbox);
- LocalToGlobal (x,y) tb
- = ((x1,y1), tb1);
- where {
- (x1,y1,tb1) = QLocalToGlobal x y tb;
- };
-
- GlobalToLocal :: !Point !Toolbox -> (!Point,!Toolbox);
- GlobalToLocal (x,y) tb
- = ((x1,y1), tb1);
- where {
- (x1,y1,tb1) = QGlobalToLocal x y tb;
- };
-
- If :: !Bool x x -> x;
- If b then else | b = then;
- = else;
-
-
- /* Calculation rules on Integers: */
-
- ABS :: !Int -> Int;
- ABS x | x >= 0 = x;
- = 0 - x;
-
- Dist :: !Int !Int -> Int;
- Dist x y | d >= 0 = d;
- = 0 - d;
- where {
- d = x - y;
- };
-
- Min :: !Int !Int -> Int;
- Min m n | m <= n = m;
- = n;
-
- Max :: !Int !Int -> Int;
- Max m n | m >= n = m;
- = n;
-
- SetBetween :: !Int !Int !Int -> Int;
- SetBetween x low up
- | x <= low = low;
- | x >= up = up;
- = x;
-
- IsBetween :: !Int !Int !Int -> Bool;
- IsBetween x low up = x >= low && x <= up;
-
-
- /* Calculation rules on Reals:
- */
-
- ABSR :: !Real -> Real;
- ABSR x | x >= 0.0 = x;
- = 0.0 - x;
-
-
- /* Calculation rules on Rectangles:
- */
-
- RectangleToRect :: !Rectangle -> Rect;
- RectangleToRect ((x,y), (x`,y`))
- | x_less_x` && y_less_y` = (x,y,x`,y`);
- | x_less_x` = (x,y`,x`,y);
- | y_less_y` = (x`,y,x,y`);
- = (x`,y`,x,y);
- where {
- x_less_x` = x <= x`;
- y_less_y` = y <= y`;
- };
-
- IsEmptyRect :: !Rect -> Bool;
- IsEmptyRect (0,0,0,0) = True;
- IsEmptyRect _ = False;
-
-
- /* List operations:
- */
-
- Head :: ![x] -> x;
- Head [x : _] = x;
-
- Tail :: ![x] -> [x];
- Tail [_ : xs] = xs;
-
- IsEmptyList :: ![x] -> Bool;
- IsEmptyList [] = True;
- IsEmptyList _ = False;
-
- Reverse :: ![x] [x] -> [x];
- Reverse [x : xs] rev = Reverse xs [x : rev];
- Reverse _ rev = rev;
-
- Concat :: ![x] ![x] -> [x];
- Concat [x : xs] ys
- = let! {
- strict1;
- } in [x : strict1];
- where {
- strict1 = Concat xs ys;
- };
- Concat _ ys = ys;
-
- Map :: !(x -> y) ![x] -> [y];
- Map f [x : xs]
- = let! {
- f_x;
- map;
- } in [f_x : map];
- where {
- f_x = f x;
- map = Map f xs;
- };
- Map _ _ = [];
-
- StateMap :: !(x -> .s -> (y, .s)) ![x] .s -> (![y], .s);
- StateMap f [x : xs] s
- = let! {
- f_xs_s1;
- f_x_s;
- } in ([y : ys], s2);
- where {
- f_x_s = f x s;
- f_xs_s1 = StateMap f xs s1;
- (y, s1) = f_x_s;
- (ys, s2) = f_xs_s1;
- };
- StateMap _ _ s = ([], s);
-
- StateMap2 :: !(x -> .s -> .s) ![x] !.s -> .s;
- StateMap2 f [x : xs] s = StateMap2 f xs (f x s);
- StateMap2 _ _ s = s;
-
- :: Cond x :== x -> Bool;
-
- Remove :: !(Cond x) x ![x] -> (!Bool, x, ![x]);
- Remove c n [x : xs]
- | c x = (True, x, xs);
- = (b, x`, [x : xs`]);
- where {
- (b, x`, xs`) = Remove c n xs;
- };
- Remove _ n xs = (False, n, xs);
-
- Append :: ![.x] !.x -> [.x];
- Append [x : xs] y
- = let! {
- xs_y;
- } in [x : xs_y];
- where {
- xs_y = Append xs y;
- };
- Append _ y = [y];
-
- Length_new :: ![x] -> Int;
- Length_new [_ : xs] = inc (Length_new xs);
- Length_new _ = 0;
-
-
- /* List operations on Integer lists:
- */
-
- RemoveCheckInt :: ![Int] !Int -> (!Bool, ![Int]);
- RemoveCheckInt [id` : ids] id
- | id == id` = (True, ids);
- = (b, [id` : ids`]);
- where {
- (b, ids`) = RemoveCheckInt ids id;
- };
- RemoveCheckInt ids _ = (False, ids);
-
- ContainsInt :: ![Int] !Int -> Bool;
- ContainsInt [c : cs] c`
- | c == c` = True;
- = ContainsInt cs c`;
- ContainsInt _ _ = False;
-
-
- // List operations on Char lists:
-
- RemoveChar :: ![Char] !Char -> [Char];
- RemoveChar [c : cs] c`
- | c == c` = cs;
- = [c : RemoveChar cs c`];
- RemoveChar cs _ = cs;
-
- ContainsChar :: ![Char] !Char -> Bool;
- ContainsChar [c : cs] c`
- | c == c` = True;
- = ContainsChar cs c`;
- ContainsChar _ _ = False;
-
-
- /* Error generation rule:
- */
- Error :: !String !String !String -> .x;
- Error rule moduleName error
- = abort ("Error in rule " +++ rule +++ " [" +++ moduleName +++ "]: " +++ error +++ ".\n");
-